<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>中心医院医疗服务系统</title>
<link rel="stylesheet" type="text/css"
	href="<c:url value="/css/themes/default/easyui.css"/>">
<link rel="stylesheet" type="text/css"
	href="<c:url value="/css/themes/icon.css"/>">
<link rel="stylesheet" type="text/css" href="<c:url value="/css/themes/yzr_style.css"/>">
<script type="text/javascript" src="<c:url value="/js/jquery.min.js"/>"></script>
<script type="text/javascript"
	src="<c:url value="/js/jquery.easyui.min.js"/>"></script>
<script type="text/javascript"
	src="<c:url value="/js/easyui-lang-zh_CN.js"/>"></script>
<style type="text/css">
ul, li {
	padding: 0px;
	margin: 0px;
	list-style: none;
}
</style>
</head>
<body class="easyui-layout">
	<script src="<c:url value="/js/jquery-migrate-1.1.0.min.js"/>"></script>
	<script type="text/javascript" src="<c:url value="/js/jquery.jqprint-0.3.js"/>"></script>
	<script type="text/javascript">
	$(function(){
		//打开新建调拨的窗口
		$("#lmb_hc_dbadd").linkbutton({onClick:function(){						
			$("#lmb_win-dbadd").window("open");
			$("#lmb_hc_bd_form").form("clear");
			$('#lmb_dbx_dg').datagrid('loadData', { total: 0, rows: [] }); 
		}});
		
		//打开选择耗材窗口
		$("#lmb-db-btn").linkbutton({onClick: function(){			
			var lmb_ck = $("#lmb-db-store").combogrid("getValue");
			if(lmb_ck == ''){
				$.messager.alert("提示", "请选择调拨仓库！");
			}else{
				$('#lmb-db-search').searchbox('setValue', '');
				$("#lmb-selectdb-win").window("open");
				$("#tb-ck-db_dg").datagrid({
		    		url: 'to_HcByCkId', 
		    		queryParams: {
		    			ckId: lmb_ck
		    		},
		    		method: 'get'
		    	});
			}			
			
		}});
		
		//显示全部
		$("#lmb-db-reload").linkbutton({onClick:function(){
			$("#tb-ck-db_dg").datagrid({
	    		url: 'to_findAllHcByCkId', 
	    		method: 'get'
	    	});			
		}});
			
		//搜索
		$('#lmb-db-search').searchbox({
		    searcher:function(value,name){
		    	if(value == ''){
		    		$.messager.alert("提示", "请输入查询条件！");
		    	}else{
		    		$("#tb-ck-db_dg").datagrid({	    		
			    		url: 'to_findAllHcByCname',
			    		method: 'post', 
			    		queryParams: {
			    			page:1,
			    			rows:13,
			    			cName: value
			    	}});
		    	}		    	
		    }
		}); 
		
		//耗材编号，耗材名，批号，单位，库存数量，结存数量，盈亏数量 
		var lmb_db_cName = undefined;//耗材名
		var lmb_db_BatchNum = undefined;//批号
		var lmb_db_storeNum = undefined;//库存数量
		
		var lmb_db_cid = undefined;//耗材id
		var lmb_db_norms = undefined;//单位
		

		$("#lmb-ok-db-selectHc").linkbutton({onClick: function(){
			var rows = $("#tb-ck-db_dg").datagrid("getSelections");
			console.log(rows);
			if(rows.length == 1){
				lmb_db_cid = rows[0].cstocCid;
				lmb_db_cName = rows[0].cstocCname;
				lmb_db_BatchNum = rows[0].cstocBatchNum;
				lmb_db_norms = rows[0].cstocNorms;
				lmb_db_storeNum = rows[0].cstocNum;
				//<!--lmb-db-cname, lmb-db-pihao, lmb-db-cid,lmb-db-norms,lmb-db-kcunnum-->
				$("#lmb-db-cname").textbox("setValue",lmb_db_cName);
				$("#lmb-db-pihao").textbox("setValue",lmb_db_BatchNum);
				$("#lmb-db-kcunnum").textbox("setValue",lmb_db_storeNum);
				$("#lmb-db-cid").textbox("setValue",lmb_db_cid);
				$("#lmb-db-norms").textbox("setValue",lmb_db_norms);
				$("#lmb-selectdb-win").window("close");
				
				
				lmb_db_cName = undefined;
				lmb_db_BatchNum = undefined;
				lmb_db_storeNum = undefined;
				
				lmb_db_cid = undefined;
				lmb_db_norms = undefined;	 
			
			}else{
				$.messager.alert("提示", "您还没有选择记录！");
			}
		}});
				
		//添加数据到datagrid--lmb_dbx_dg
		$("#lmb-db-btn-add").linkbutton({onClick: function(){
			//验证表单是否完善
			if($('#lmb_hc_bd_form').form("validate")){
				//库存数量
				var lmb_num = $("#lmb-db-kcunnum").textbox("getValue");		
				//调拨
				var lmb_db_num = $("#lmb-db-dbnum").numberbox("getValue");
				if(lmb_num == ''||lmb_db_num==''){
					alert("请完善所有数据！");
				}else{
					//<!--lmb-db-cname, lmb-db-pihao, lmb-db-cid,lmb-db-norms,lmb-db-kcunnum-->
					if(eval(lmb_db_num)>0){
						if(eval(lmb_db_num) <= eval(lmb_num)){
							$('#lmb_dbx_dg').datagrid('appendRow',{
								aCid: $("#lmb-db-cid").textbox("getValue"),
								aCname: $("#lmb-db-cname").textbox("getValue"),
								aBatchNum: $("#lmb-db-pihao").textbox("getValue"),
								aNorms: $("#lmb-db-norms").textbox("getValue"),
								ckNum:$("#lmb-db-kcunnum").textbox("getValue"),
								aNum: $("#lmb-db-dbnum").numberbox("getValue"),
							});
							
							//清除部分数据
							$("#lmb-db-cid").textbox("clear"),
							$("#lmb-db-cname").textbox("clear"),
							$("#lmb-db-pihao").textbox("clear"),
							$("#lmb-db-norms").textbox("clear"),
							$("#lmb-db-kcunnum").textbox("clear"),
							$("#lmb-db-dbnum").numberbox("clear")
						}else{
							$.messager.alert("提示", "调拨数量大于库存数量！");
						}
					}else{
						$.messager.alert("提示", "调拨数量不能小于0");
					}
						
				}				
			}else{
				alert("请完善所有数据！");
			}		
		}});	
		
		//删除行
		$("#lmb-dbDet-btn—cancel").linkbutton({onClick: function(){
			var rows = $("#lmb_dbx_dg").datagrid("getSelections");			
			var lmb_db_index =$('#lmb_dbx_dg').datagrid('getRowIndex', rows[0]);
			$('#lmb_dbx_dg').datagrid('deleteRow',lmb_db_index);
		}});
		
		
		
		
		//提交数据到数据库//lmb-pandianDet-tijiao
		$("#lmb-dbDet-tijiao").linkbutton({onClick: function(){			
			var rows = $("#lmb_dbx_dg").datagrid("getRows");
			console.log(JSON.stringify(rows));

			//耗材编号，耗材名称，批号，单位，库存数量，报损数量
			//<!-- cstocCid,cstocCname,cstocBatchNum,cstocNorms,ckNum,cstocNum-->
			for(var i = 0; i < rows.length; i ++){
				generateHiddenLmb_diaobo(rows[i], "aCid", i);
				generateHiddenLmb_diaobo(rows[i], "aCname", i);
				generateHiddenLmb_diaobo(rows[i], "aBatchNum", i);
				generateHiddenLmb_diaobo(rows[i], "aNorms", i);
				generateHiddenLmb_diaobo(rows[i], "aNum", i);
			}
					
			$('#lmb_hc_bd_form').form('submit', {				
				url: 'toLmbDoHcdb', 
				onSubmit: function(){
					var jg=$('#lmb_hc_bd_form').form("validate");
			    	if(jg==false){
			    		$.messager.alert("提示", "请完善所有数据！");
			    		return false;
			    	}
			    }, 
				success: function(v){
					$.messager.alert('提示', "提交成功");
					$('#lmb_win-dbadd').window('close');
					$(":hidden[x='y']").remove();			
					$('#lmb_hc_bd_form').form("clear");
					$('#lmb_dbx_dg').datagrid('loadData', { total: 0, rows: [] }); 		
					$("#lmb-hcdb").datagrid("reload");
				}
			});
		}});

		//创建隐式表单域来保存明细
		function generateHiddenLmb_diaobo(row, name, i){
			var lmb_hc_bd_form = $('#lmb_hc_bd_form');
			//type='hidden'
			$("<input type='hidden' x='y'>")
				.attr("name", "applyDet[" + i + ']' + '.' + name)
				.val(row[name])
				.appendTo(lmb_hc_bd_form);
		}	
	});
	
</script> 
<script>
	$(function(){
		//显示全部
		$("#lmb_hc_dbsx").linkbutton({onClick:function(){
			$("#lmb-hcdb").datagrid({
	    		url: 'toLmbfindAllDb', 
	    		method: 'get'
	    	});			
		}});
		
		//日期转换
		function lmb_todate_db(value){
            if(value){
				var date = new Date(value);  
                return date.toLocaleDateString();
			}
        }
		
		$("#lmb_hc_dbxq").linkbutton({onClick: function(){
			alert(3);
			var rows = $("#lmb-hcdb").datagrid("getSelections");
			if(rows.length == 1){	  				
				$('#lmb-hcdbDet-window').window("open");	
				var allotId = rows[0].allotId;
				console.log(allotId);
				//<!-- lmb-hcdbDet-dbbh， lmb-hcdbDet-dnsj，lmb-hcdbDet-cgy，lmb-hcdbDet-dbc，lmb-hcdbDet-dbz-->
				$.getJSON("toLmbfindOneDb", {"allotId": allotId}, function(data){
					$("#lmb-hcdbDet-dbbh").linkbutton({text: data.allotId});
					$("#lmb-hcdbDet-dnsj").linkbutton({text: lmb_todate_db(data.allotApplyTime)});
					$("#lmb-hcdbDet-cgy").linkbutton({text: data.cgName});
					$("#lmb-hcdbDet-dbc").linkbutton({text: data.dbcName});
					$("#lmb-hcdbDet-dbz").linkbutton({text: data.dbtoName});
				});	
				
				 //根据id查看详情	
				$("#lmb-hcdbDet-dg").datagrid({
					url:'toLmbfindXqBydbId',
					method:'post',
					queryParams:{
						id:allotId
					}
				});
			}else{
				$.messager.alert("提示", "只能选择一条记录，您目前选择了" + rows.length + "条!");
			}
		}});
		
		//点击按钮打印
		$("#lmb-hcdbDet-print").linkbutton({
			onClick: function(){
				$("#lmb_hcdbDet_form").jqprint();								
			}			
		});
	});
</script>

	<table id="lmb-hcdb" class="easyui-datagrid" data-options="
			url:'toLmbfindDbByPage',
			fitColumns:true,fit:true,
			pagination: true,
			pageSize: 5,
			pageList: [5, 10, 15, 20],
			rownumbers: true,
			toolbar: '#lmb_hc_diaobo',
			border:false
			">
		<thead>
			<tr>
				<th data-options="field:'',checkbox:true"></th>
				<th data-options="field:'allotId',width:'30%'">调拨编号</th>
				<th data-options="field:'allotApplyTime', width:'17%',
						formatter:function(value){
                          if(value){
							var date = new Date(value);  
                            return date.toLocaleDateString();
						  }
                 }">调拨时间</th>     
				<th data-options="field:'cgName',width:'17%'">仓管员</th>
				<th data-options="field:'dbcName',width:'17%'">调拨仓库</th>						
				<th data-options="field:'dbtoName',width:'17%'">调拨至</th>	
			</tr>
		</thead>
	</table>
	
	<div id="lmb_hc_diaobo">
		<a id="lmb_hc_dbadd" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true">添加调拨记录</a>
		<a id="lmb_hc_dbxq" class="easyui-linkbutton" data-options="iconCls:'icon-xiangqing',plain:true">调拨详情</a>
		<a id="lmb_hc_dbsx" class="easyui-linkbutton" data-options="iconCls:'icon-reload',plain:true">刷新</a>
	</div> 	
	
	<div id="lmb_win-dbadd" class="easyui-window" title="录入调拨信息" data-options="collapsible:true,minimizable:false,maximizable:true,modal:true,iconCls:'icon-save',closed: true" style="width:800px;height:320px;padding:5px;">
		<form id="lmb_hc_bd_form" method="post">
			<table width="100%" style="background: rgb(236,243,255); padding: 10px; border-radius: 5px;">
				<tr>
					<td width="50%">
						<input id="lmb_db_dbrq" class="easyui-datebox" name="allotApply.allotApplyTime"
							data-options="label:'调拨日期',width:'60%',required:true,labelAlign:'right'">
					</td>
					<td width="50%">
						<select id="lmb_db_cgy" name="allotApply.allotCgy" class="easyui-combogrid" 
							    data-options="
							    required:true,
							    label: '仓管员',	
							    labelAlign:'right',						  
							    width:'88%',
							    panelWidth:288,
							    required:true,
							    url: 'queryAllStaff',
								pagination: true,
								pageSize: 8,
								pageList: [8, 16, 24, 32],
								idField: 'staffNo',
								textField: 'staffName',
								columns: [[
									{field:'staffName',title:'姓名',width:100},
									{field:'departmentName',title:'部门',width:100},
									{field:'postName',title:'职务',width:85}
								]]
						"></select>
					</td>			
				</tr>
				<tr>
					<td>
						<select id="lmb-db-store"  name="allotApply.allotStorehouse" class="easyui-combogrid"
					data-options="
						label: '调拨仓库',
						labelAlign:'right',
						required:true,
						width: '60%',
						panelWidth: '290',
						panelHeight: 'auto',				
						url: 'queryAllStorehouse',
						pagination: true,
						pageSize: 8,
						pageList: [8, 16, 24, 32],
						idField: 'storeId',
						textField: 'storeName',
						columns: [[
							{field:'storeName',title:'仓库名',width:100},
							{field:'storeKind',title:'类型',width:80},
							{field:'storeAddress',title:'地址 ',width:100}
						]]
					"></select>						
					</td>
					
					<td>
						<select id="lmb-db-tostore"  name="allotApply.allotToStorehouse" class="easyui-combogrid"
					data-options="
						label: '调拨至',
						labelAlign:'right',
						required:true,
						width: '60%',
						panelWidth: '290',
						panelHeight: 'auto',				
						url: 'queryAllStorehouse',
						pagination: true,
						pageSize: 8,
						pageList: [8, 16, 24, 32],
						idField: 'storeId',
						textField: 'storeName',
						columns: [[
							{field:'storeName',title:'仓库名',width:100},
							{field:'storeKind',title:'类型',width:80},
							{field:'storeAddress',title:'地址 ',width:100}
						]]
					"></select>
					</td>	
				</tr> 
				<tr>					
					<td>
						<input  id="lmb-db-cname" class="easyui-textbox"  data-options="width:'60%',labelAlign:'right',label:'耗材',readonly:'readonly'">	
						<a id="lmb-db-btn" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search'">查找</a>				
					</td>
					<td>
						<input id="lmb-db-pihao" class="easyui-textbox"  data-options="width:'60%',labelAlign:'right',label:'批号',readonly:'readonly'">					
					</td>
				</tr> 
				<tr>			
					<td>
						<input id="lmb-db-cid" class="easyui-textbox"  data-options="width:'60%',labelAlign:'right',label:'耗材编号',readonly:'readonly'">	
					</td>
					<td>
						<input id="lmb-db-norms" class="easyui-textbox"  data-options="width:'60%',labelAlign:'right',label:'单位',readonly:'readonly'">	
					</td>
				</tr>
				<tr>			
					<td>
						<input id="lmb-db-kcunnum" class="easyui-textbox"  data-options="width:'60%',labelAlign:'right',label:'库存数量',readonly:'readonly'">	
					</td>
					<td>
						<input id="lmb-db-dbnum" class="easyui-numberbox"  data-options="width:'60%',labelAlign:'right',label:'调拨数量'">	
						<a id="lmb-db-btn-add" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true">添加</a>
					</td>
				</tr> 				
			</table>
			<div style="margin: 5px;">
				<div id="lmb_dbx_tab" class="easyui-tabs" style="width:100%;margin-top:10px;"
					data-options="plain:true,narrow:true">
					<div id="lmb_dbx_tab_1" title="调拨明细" style="padding:10px">
						<table id="lmb_dbx_dg" class="easyui-datagrid" style="height:auto"
							data-options="
								iconCls:'icon-edit',
								singleSelect: true,
								toolbar: '#lmb_db_tb',
								method: 'get',								
								width: '100%'
							">
							<thead>
								<tr>
									<th data-options="field:'aCid',width:'20%',align:'center'">耗材编号</th>			
									<th data-options="field:'aCname',width:'18%',align:'center'">耗材名称</th>
									<th data-options="field:'aBatchNum',width:'14%',align:'center'">批号</th>
									<th data-options="field:'aNorms',width:'12%',align:'center'">单位</th>
									<th data-options="field:'ckNum',width:'12%',align:'center'">库存数量</th>
									<th data-options="field:'aNum',width:'12%',align:'center'">调拨数量</th>
								</tr>
							</thead>
						</table>
						<div id="lmb_db_tb" style="height:auto">
							<a id="lmb-dbDet-tijiao" href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-ok'">提交</a>
							<a id="lmb-dbDet-btn—cancel" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true">删除一行</a>			
						</div>
					</div>				
				</div>				
			</div>
		</form>
	</div>
	
	<div id="lmb-selectdb-win" class="easyui-window" title="选择调拨耗材" style="width:850px;height:350px;" data-options="closable:true,closed:true,collapsible:false,minimizable:false,maximizable:false">
		<table id="tb-ck-db_dg" class="easyui-datagrid" 
			data-options="			
				fit:true,
				pagination: true,
				pageSize: 8,
				pageList: [8, 16, 24, 32],
				rownumbers: false,
				toolbar: '#lmb-toolbar-diaobo',
				border:false,
				singleSelect:true
			">
			<thead>
				<tr>				
					<th data-options="field:'ckName',width:'20%',align:'center',editor:'textbox'">仓库</th>				
					<th data-options="field:'cstocCid',width:'20%',align:'center',editor:'textbox'">耗材编号</th>				
					<th data-options="field:'cstocCname',width:'20%',align:'center'">耗材名称</th>
					<th data-options="field:'cstocBatchNum',width:'13%',align:'center'">批号</th>					
					<th data-options="field:'cstocNorms',width:'13%',align:'center'">单位</th>	
					<th data-options="field:'cstocNum',width:'13%',align:'center'">数量</th>																								
				</tr>
			</thead>
		</table>				    
	    
	    <!-- 工具栏 -->
	    <div id="lmb-toolbar-diaobo">
	    	<input id="lmb-db-search" class="easyui-searchbox" data-options="menu: '#lmb-diaobo-serch', width: 250, prompt: '请输入关键字'"/>    	
	    	<a id="lmb-db-reload" class="easyui-linkbutton" data-options="iconCls:'icon-reload',plain:true">刷新</a>
	    	<a id="lmb-ok-db-selectHc" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-ok'">确定</a>
	    </div>
	    	    
	    <!-- 搜索框 -->
	    <div id="lmb-diaobo-serch" style="width:120px">
	    	<div data-options="name:'cName'">耗材名称</div>
		</div>		
	</div>			

	<!--查看详情窗口-打印 -->
	<div id="lmb-hcdbDet-window" class="easyui-window" title="耗材调拨详情" data-options="collapsible:false,minimizable:false,maximizable:false,modal:true,iconCls:'icon-save',closed: true" style="width:70%;height:70%;">		
		<div class="easyui-layout" style="padding:0px;" data-options="fit:true">			
			<div id="lmb-hcdbDet-printArea" data-options="region:'center'" style="padding:5px;border-top:none;border-left:none;border-right:none;">			
				<form id="lmb_hcdbDet_form" method="post">
					<h1 style="text-align: center">耗材调拨详情</h1>
					<table width="100%">
						<tr>
							<td>调拨编号: <a id="lmb-hcdbDet-dbbh" name="allotId"     class="easyui-linkbutton" data-options="plain:true,"></a></td>
							<td>调拨时间: <a id="lmb-hcdbDet-dnsj" name="allotApplyTime"  class="easyui-linkbutton" data-options="plain:true"></a></td>
							<td>仓管员:  <a id="lmb-hcdbDet-cgy"  name="cgName"        class="easyui-linkbutton" data-options="plain:true"></a></td>
						</tr>
						<tr>
							<td>调拨仓:<a id="lmb-hcdbDet-dbc" name="dbcName" class="easyui-linkbutton" data-options="plain:true"></a></td>
							<td>调拨至:<a id="lmb-hcdbDet-dbz" name="dbtoName" class="easyui-linkbutton" data-options="plain:true"></a></td>
						</tr>
					</table>
						
					<div style="margin: 5px;" style="width:100%">
						<table id="lmb-hcdbDet-dg" class="easyui-datagrid" 
							data-options="
								iconCls: 'icon-edit',
								singleSelect: true,										
								width: '100%'						
						">			
							<thead>
								<tr>	
									<th data-options="field:'aCid',width:'25%',align:'center'">耗材编号</th>			
									<th data-options="field:'aCname',width:'25%',align:'center'">耗材名称</th>
									<th data-options="field:'aBatchNum',width:'17%',align:'center'">批号</th>
									<th data-options="field:'aNorms',width:'17%',align:'center'">单位</th>
									<th data-options="field:'aNum',width:'16%',align:'center'">调拨数量</th>
								</tr>
							</thead>
						</table>									
					</div>
				</form>				
			</div>
					
			<div data-options="region:'south',border:false" style="text-align:right;padding:5px 0 0;">
				<a id="lmb-hcdbDet-print"  class="easyui-linkbutton" data-options="iconCls:'icon-print', plain:true">打印</a>
				<a class="easyui-linkbutton" data-options="iconCls:'icon-cancel', plain:true" href="javascript:$('#lmb-hcdbDet-window').window('close');">取消</a>
			</div>			
		</div>
	</div>
</body>
</html>